﻿
@{
    Layout = "";
}
<style>
    .ngdialog.ngdialog-theme-plain .ngdialog-content {
        width: 45% !important;
    }
</style>

<div ng-controller="StudentBedDetailController">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <form name="formValidate" ng-submit="submitForm()" novalidate="" class="form-validate form-horizontal">
                    <!-- START panel-->
                    <div class="panel panel-default" ng-class="{ 'whirl standard': isLoading,'': isLoading }">
                        <div class="panel-body">
                            <fieldset class="b0">
                                <legend>{{StudentBed.Id == 0 || StudentBed.Id == null ? '添加学生对应床位' : '修改学生对应床位'}}</legend>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">楼栋</label>
                                    <div class="col-sm-4">
                                        <select ng-options="Building as Building.BuildingNO for Building in BuildingList track by Building.Id" ng-model="Building" class="form-control" name="BuildingNO" id="sel_Building" ng-blur="BuildingChange()">
                                            <option value="">请选择楼栋</option>
                                        </select>
                                        <span ng-show="validateInput('BuildingNO', 'required')" class="text-danger">请选择楼栋</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">公寓号</label>
                                    <div class="col-sm-4">
                                        <select ng-options="Room as Room.RoomNO for Room in RoomList track by Room.Id" ng-model="Room" class="form-control" name="RoomNO" id="sel_Room" ng-change ="GetBedInfo(Room.Id)" ng-blur="RoomChange()">
                                            <option value="">请选择公寓</option>
                                        </select>
                                        <span ng-show="validateInput('RoomNO', 'required')" class="text-danger">请选择公寓</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">学院</label>
                                    <div class="col-sm-4">
                                        <select ng-disabled="StudentBed.Id == 0 || StudentBed.Id == null ? '': 'disabled'" ng-options="Department.Id as Department.DepartmentName for Department in DepartmentList" ng-model="StudentBed.DepartmentId" class="form-control" name="DepartmentName" id="sel_Department" ng-blur="DepartmentChange()">
                                            <option value="">请选择学院</option>
                                        </select>
                                        <span ng-show="validateInput('BuildingNO', 'required')" class="text-danger">请选择楼栋</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">班主任</label>
                                    <div class="col-sm-4">
                                        <select ng-disabled="StudentBed.Id == 0 || StudentBed.Id == null ? '': 'disabled'" ng-options="Teacher.Id as Teacher.RealName for Teacher in TeacherList" ng-model="StudentBed.TeacherId" class="form-control" name="TeacherName" id="sel_Teacher">
                                            <option value="">请选择班主任</option>
                                        </select>
                                        <span ng-show="validateInput('TeacherName', 'required')" class="text-danger">请选择班主任</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">班级</label>
                                    <div class="col-sm-4">
                                        <select ng-disabled="StudentBed.Id == 0 || StudentBed.Id == null ? '': 'disabled'" ng-options="Class.Id as Class.ClassName for Class in ClassList" ng-model="StudentBed.ClassId" class="form-control" name="ClassName" required="" id="sel_Class">
                                            <option value="">请选择班级</option>
                                        </select>
                                        <span ng-show="validateInput('ClassName', 'required')" class="text-danger">请选择班级</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>                            
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">学生学号</label>
                                    <div class="col-sm-4">
                                        <input ng-disabled="StudentBed.Id == 0 || StudentBed.Id == null ? '': 'disabled'" type="text" name="StudentNO" id="StudentNO" required="" ng-model="StudentNO" class="form-control" ng-blur ="GetStudentInfo(StudentNO)"/>
                                        <span ng-show="validateInput('StudentNO', 'required')" class="text-danger">请输入学生学号</span>
                                        <span ng-show="IsRightStudentNO" class="text-danger">学生学号只能为数字</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">学生姓名</label>
                                    <div class="col-sm-4">
                                        <input ng-disabled="StudentBed.Id == 0 || StudentBed.Id == null ? '': 'disabled'" type="text" name="StudentName" id="StudentName" required="" ng-model="StudentName" class="form-control" readonly />
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group" >
                                    <label class="col-sm-2 control-label">学生性别</label>
                                    <label class="radio-inline c-radio">
                                        <input type="radio" disabled name="StudentSex" ng-value="{{true}}" ng-model="Sex" />
                                        <span class="fa fa-circle"></span>男
                                    </label>
                                    <label class="radio-inline c-radio">
                                        <input type="radio" disabled name="StudentSex" ng-value="{{false}}" ng-model="Sex" />
                                        <span class="fa fa-circle"></span>女
                                    </label>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">床号</label>
                                    <div class="col-sm-4">
                                        <select ng-options="Bed as Bed.BedNO for Bed in BedList track by Bed.Id" ng-model="Bed" class="form-control" name="BedNO" required="" id="sel_Bed" ng-change="BedNOChange(Bed.Id)">
                                            <option value="">请选择床号</option>
                                        </select>
                                        <span ng-show="validateInput('BedNO', 'required')" class="text-danger">请选择床号</span>
                                        <span ng-show="IsRightBedNO" class="text-danger">该床位已经住人，请重新选择床位</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            
                        </div>
                        <div class="panel-footer text-right">
                            <button type="button" class="btn btn-warning" ng-click="cancel()">返回列表</button>
                            <button type="button" class="btn btn-primary" ng-click="save()">保存</button>
                        </div>
                        
                    </div>
                    <!-- END panel-->
                </form>
            </div>
        </div>
    </div> 
</div>


<div id='container' ng-show="false"></div>


<link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css" />
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.0&key=56aaa1c52d140b9b3276d2893997b0ba"></script>
<script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>

<script type="text/javascript">
    App.controller('StudentBedDetailController', ["$scope", "$state", "$http", "$stateParams", "Notify", "$filter",
        function ($scope, $state, $http, $stateParams, Notify, $filter) {
            'use strict';
            $scope.isLoading = true;
            $scope.BedId = 0;
            $scope.IsRightStudentNO = false;
            $scope.IsCheckStudentNO = false;
            $scope.StudentNO = "";
            $scope.StudentId = 0;
            $scope.StudentName = "";
            $scope.Student = {};
            $scope.IsRightBedNO = false;

            $scope.validateInput = function (name, type) {
                var input = $scope.formValidate[name];
                return (input.$dirty || $scope.submitted) && input.$error[type];
            };

            //数据初始化
            $http.post("/StudentBed/GetStudentBedInitDetail", { id: $stateParams.id }).then(function (response) {
                
                $scope.StudentBed = angular.copy(response.data.StudentBed);
                $scope.Building = angular.copy(response.data.BuildingInfo);
                $scope.Room = angular.copy(response.data.RoomInfo);
                $scope.Bed = angular.copy(response.data.BedInfo);


                if ($stateParams.id != 0)
                {
                    $scope.BedList = response.data.BedList;
                    $scope.BedAllList = response.data.BedList;
                    $scope.RoomChange();
                }

                $scope.StudentInfo = angular.copy(response.data.StudentInfo);

                if ($scope.StudentInfo != null)
                {
                    $scope.StudentNO = $scope.StudentInfo.UserName;
                    $scope.StudentName = $scope.StudentInfo.RealName;
                    $scope.Student.Sex = $scope.StudentInfo.Sex;
                    $scope.StudentId = $scope.StudentInfo.Id;
                    if ($scope.Student.Sex == 1) {
                        $scope.Sex = true;
                    }
                    else {
                        $scope.Sex = false;
                    }
                }
                
                $scope.BuildingList = angular.copy(response.data.BuildingList);
                $scope.RoomList = angular.copy(response.data.RoomList);
                $scope.RoomAllList = angular.copy(response.data.RoomList);
                $scope.DepartmentList = angular.copy(response.data.DepartmentList);

                $scope.TeacherList = angular.copy(response.data.TeacherList);
                $scope.TeacherAllList = angular.copy(response.data.TeacherList);

                $scope.ClassList = angular.copy(response.data.ClassList);
                $scope.ClassAllList = angular.copy(response.data.ClassList);
                
                
                if ($scope.StudentBed == null) {
                    $scope.StudentBed = {};
                }

                $scope.isLoading = false;
            });
            
            // 保存点击
            $scope.save = function () {

                $scope.submitted = true;                
                var testStudentNO = /^[0-9]*$/;
                if (testStudentNO.test($scope.StudentNO) === false) {
                    $scope.IsRightStudentNO = true;
                    var txt = document.getElementById('StudentNO')
                    txt.focus();
                    txt.select();
                    return false;
                } else {
                    $scope.IsRightStudentNO = false;
                }

                if ($scope.StudentId == 0) {
                    Notify.alert("请输入学生信息", { status: 'warning' });
                    return;
                }

                if ($scope.Bed.Id == 0) {
                    Notify.alert("请先选择有的床位大楼和公寓后，再选择床位", { status: 'warning' });
                    return;
                }

                //检查公寓是否存在
                $http.post("/StudentBed/StudentBedCheck", { BedId: $scope.Bed.Id }).then(function (returnData) {
                    if (returnData.data.isSuccess == false) {                        
                        $scope.IsRightBedNO = false;
                        $scope.IsCheckStudentNO = false;
                        //保存公寓信息
                        
                        $scope.StudentBedSubmit();
                        
                    } else {
                        $scope.IsRightBedNO = true;
                        $scope.IsCheckStudentNO = true;
                        var sel = document.getElementById('sel_Bed');
                        sel.focus();
                        
                    }
                });
            };

            //返回列表
            $scope.cancel = function () {
                $state.go("app.studentbedlist");
            };
            //获取床位和柜号信息
            $scope.GetBedInfo = function (RoomId) {
                
                $http.post("/StudentBed/GetBedInfo", { RoomId: RoomId }).then(function (returnData) {   
                    $scope.BedList = returnData.data.BedList;
                    $scope.BedAllList = returnData.data.BedList;
                    return true;
                });
            };

            //获取学生信息
            $scope.GetStudentInfo = function (StudentNO) {
                $http.post("/StudentBed/GetStudentInfo", { StudentNO: StudentNO }).then(function (returnData) {
                    
                    if (returnData.data.isSuccess == true)
                    {
                        
                        $scope.StudentId = returnData.data.data.Id;
                        $scope.StudentName = returnData.data.data.RealName;
                        $scope.StudentBed.StudentId = returnData.data.data.Id;
                        if($scope.Student.Sex == 1)
                        {
                            $scope.Sex = true;
                        }
                        else {
                            $scope.Sex = false;
                        }
                    }
                    else
                    {
                        Notify.alert(returnData.data.msg, { status: 'warning' });
                    }
                    
                });
            };


            //通过改变楼宇信息，获取公寓信息
            $scope.BuildingChange = function () {
                $scope.RoomList = $scope.RoomAllList;
                var selIndex = document.getElementById("sel_Building").selectedIndex;
                if (selIndex == 0) {

                }
                else {
                    $scope.RoomList = $scope.RoomList.filter(function (x) {

                        return x.BuildingId === $scope.Building.Id;
                    });
                }
            };

            //通过改变学院信息，获取老师班级信息
            $scope.DepartmentChange = function () {
                $scope.ClassList = $scope.ClassAllList;
                $scope.TeacherList = $scope.TeacherAllList;
                var selIndex = document.getElementById("sel_Department").selectedIndex;
                if (selIndex == 0) {

                }
                else {
                    $scope.ClassList = $scope.ClassList.filter(function (x) {
                        return x.DepartmentId === $scope.StudentBed.DepartmentId;
                    });
                    $scope.TeacherList = $scope.TeacherList.filter(function (x) {
                        return x.DepartmentId === $scope.StudentBed.DepartmentId;
                    });
                }
            };

            //通过改变公寓信息，获取床位信息
            $scope.RoomChange = function () {

                $scope.BedList = $scope.BedAllList;
                var selIndex = $scope.Bed.Id;
                if (selIndex == -1) {

                }
                else {
                    
                    $scope.BedList = $scope.BedList.filter(function (x) {
                        return x.RoomId === $scope.Room.Id;
                    });
                }
                if ($scope.BedList.length == 0) {
                    $scope.Bed.BedId = 0;
                }
            };
            //改变床位时去掉判断语句
            $scope.BedNOChange = function (Bed) {
                $scope.IsRightBedNO = false;
                $scope.StudentBed.BedId = Bed.Id;
            };

            //学生对应床位提交后台
            $scope.StudentBedSubmit = function () {

                $scope.StudentBed.BuildingId = $scope.Building.Id;
                $scope.StudentBed.RoomId = $scope.Room.Id;
                $scope.StudentBed.BuildingId = $scope.Building.Id;
                $scope.StudentBed.BedId = $scope.Bed.Id;

                $scope.isLoading = true;
                if ($scope.formValidate.$valid) {
                    $http.post("/StudentBed/StudentBedSave", $scope.StudentBed).then(function (returnData) {
                        if (returnData.data.isSuccess) {
                            Notify.alert('操作成功!', { status: 'success' });
                            $state.go("app.studentbedlist");
                        } else {
                            Notify.alert(returnData.data.msg, { status: 'warning' });
                            $scope.isLoading = false;
                        }
                    });

                } else {
                    $scope.isLoading = false;
                }
            };

        }]);
</script>
